import React from 'react';
import {Badge, Button, Card, Checkbox, Form, Input, DatePicker, Select, Table} from 'antd';
import Utils from './../../utils/utils';


const FormItem = Form.Item;
const Option = Select.Option;

export default class ETable extends React.Component {


    //单选选中行
    onRowClick = (record, index) => {
        let rowSelection = this.props.rowSelection;
        if (rowSelection === 'checkbox') {
            let selectedRowKeys = this.props.selectedRowKeys;
            let selectedItem = this.props.selectedItem;
            let selectedIds = this.props.selectedIds;
            if (selectedIds) {
                const i = selectedIds.indexOf(record.id);
                if (i == -1) {
                    selectedIds.push(record.id);
                    selectedRowKeys.push(index);
                    selectedItem.push(record);
                } else {
                    //取消选中
                    selectedIds.splice(i, 1);
                    selectedRowKeys.splice(i, 1);
                    selectedItem.splice(i, 1);

                }
            } else {
                selectedIds = [record.id]
                selectedRowKeys = [index]
                selectedItem = [record]

            }
            this.props.updateSelectedItem(selectedRowKeys, selectedItem, selectedIds)


        } else {
            let selectedRowKeys = [index]
            let selectedItem = record
            //子组件给父组件传值只需要调父组件的方法就行
            this.props.updateSelectedItem(selectedRowKeys, selectedItem)
            console.log('record', record)

        }

    };


    tableInit = () => {
        let selectRow = this.props.rowSelection;
        let selectedRowKeys = this.props.selectedRowKeys;
        const rowSelection = {
            type: 'radio',
            selectedRowKeys,
            onChange: this.onSelectChange

        };

        if (selectRow === false && selectRow === null) {
            selectRow = false

        } else if (selectRow === 'checkbox') {
            rowSelection.type = 'checkbox';
        }

        return <Table
            bordered
            {...this.props}
            rowSelection={selectRow ? rowSelection : null}
            onRow={(record, index) => {
                return {
                    onClick: () => {
                        //防止没单选复选也点击了改行而触发事件
                        if (!selectRow) {
                            return;
                        }
                        this.onRowClick(record, index)
                    },   //点击行

                }
            }}
        />

    };

    render() {
        return (
            <div>
                {this.tableInit()}
            </div>
        );
    }


}
